import java.util.Stack;

public class Solution {
    public String removeDuplicates(String str) {
        int len = str.length();
        Stack<Character> s = new Stack<>();
        for (int i = 0; i < len; i++) {
            if (s.isEmpty()) {
                s.push(str.charAt(i));
            } else {
                char ch = s.peek();
                if (ch == str.charAt(i)) {
                    s.pop();
                } else {
                    s.push(str.charAt(i));
                }
            }
        }
        StringBuilder ret = new StringBuilder();
        while (!s.isEmpty()) {
            ret.append(s.pop());
        }
        return ret.reverse().toString();
    }
}
